package com.youlai.boot.gym.controller;

import com.youlai.boot.gym.service.MembersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.gym.model.form.MembersForm;
import com.youlai.boot.gym.model.query.MembersQuery;
import com.youlai.boot.gym.model.vo.MembersVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 会员管理前端控制层
 *
 * @author hxc
 * @since 2025-06-01 11:27
 */
@Tag(name = "会员管理接口")
@RestController
@RequestMapping("/api/v1/members")
@RequiredArgsConstructor
public class MembersController  {

    private final MembersService membersService;

    @Operation(summary = "会员管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('gym:members:query')")
    public PageResult<MembersVO> getMembersPage(MembersQuery queryParams ) {
        IPage<MembersVO> result = membersService.getMembersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增会员管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('gym:members:add')")
    public Result<Void> saveMembers(@RequestBody @Valid MembersForm formData ) {
        boolean result = membersService.saveMembers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取会员管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('gym:members:edit')")
    public Result<MembersForm> getMembersForm(
        @Parameter(description = "会员管理ID") @PathVariable Long id
    ) {
        MembersForm formData = membersService.getMembersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改会员管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('gym:members:edit')")
    public Result<Void> updateMembers(
            @Parameter(description = "会员管理ID") @PathVariable Long id,
            @RequestBody @Validated MembersForm formData
    ) {
        boolean result = membersService.updateMembers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除会员管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('gym:members:delete')")
    public Result<Void> deleteMemberss(
        @Parameter(description = "会员管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = membersService.deleteMemberss(ids);
        return Result.judge(result);
    }
}
